package org.etlt.check;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.etlt.job.JobContext;
import org.etlt.job.SettingResolver;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ChecklistFactory {
    protected Log log = LogFactory.getLog(getClass());

    private static final ChecklistFactory CHECKLIST_FACTORY = new ChecklistFactory();

    private ChecklistFactory(){}

    public static ChecklistFactory getInstance(){
        return CHECKLIST_FACTORY;
    }

    private final SettingResolver<CheckListSetting> settingResolver = new CheckListSettingXmlResolver();

    public CheckList resolveInstance(String settingFile, JobContext context) throws IOException {
        File file = new File(context.getContextRoot(), settingFile);
        log.info("start: resolve checklist from file: " + file);
        InputStream stream = new FileInputStream(file);
        CheckListSetting setting = this.settingResolver.resolve(stream, context);
        CheckList checkList = createChecklist(setting);
        log.info("end: resolve checklist from file: " + file);
        return checkList;
    }

    public CheckList createChecklist(CheckListSetting setting){
        return new CheckList(setting);
    }
}
